package jee.boot.module.act.service;

import jee.boot.common.basic.CrudService;
import jee.boot.common.basic.Page;
import jee.boot.common.basic.ServiceException;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.activiti.ActTask;
import jee.boot.entity.auth.Role;
import jee.boot.entity.auth.User;
import jee.boot.entity.system.Filter;
import jee.boot.module.act.dao.ActTaskDao;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ActTaskService extends CrudService<ActTaskDao, ActTask> {
    @Autowired
    private IdentityService identityService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private org.activiti.engine.TaskService taskService;
    @Autowired
    private AuthorityHandler authorityHandler;

    public void complete(ActTask task) {

        if (StringUtils.isBlank(task.getId())) {
            throw new ServiceException(1420);
        }
        if (StringUtils.isBlank(task.getProcInst())) {
            throw new ServiceException(1421);
        }
        // 添加意见
        String comment = task.getComment();
        taskService.addComment(task.getId(), task.getProcInst(), comment);
        taskService.setAssignee(task.getId(), authorityHandler.getUserId());// 审批时候默认设置下是当前登录人信息
        // 提交任务
        taskService.complete(task.getId(), task.getVariable().toMap());

    }

    public void delegate(ActTask task) {
        taskService.delegateTask(task.getId(), task.getAssignee());
    }

    public void claim(ActTask task) {
        taskService.claim(task.getId(), task.getAssignee());
    }

    public void unclaim(ActTask task) {
        taskService.unclaim(task.getId());
    }

    @Override
    public void delete(ActTask task) {
        taskService.deleteTask(task.getId());
    }

    /**
     * 个人待办任务列表
     *
     * @param entity
     * @return
     */
    @Override
    public Page<ActTask> findPage(ActTask entity) {
        User currentUser = authorityHandler.getCurrentUser();
        if (currentUser != null) {
            List<Role> roleList = authorityHandler.getRoleList();
            List<Map<String, Object>> filterList = Lists.newArrayList();
            // 任务受让人,即当前的任务审核人员
            filterList.add(new Filter("assignee", "=", currentUser.getId()).toMap());
            // 任务委托人,即当前登录人为委托人,帮助别人审核的数据
            filterList.add(new Filter("owner", "=", currentUser.getId()).toMap());
            // 任务候选人
            filterList.add(new Filter("candidateUser.userJson", "like", "\"" + currentUser.getId() + "\"").toMap());
            // 任务候选角色
            if (roleList != null && !roleList.isEmpty()) {
                roleList.forEach(role -> {
                    filterList.add(new Filter("candidateGroup.groupJson", "like", "\"" + role.getCode() + "\"").toMap());
                });
            }
            entity.addFilter(filterList);
        } else {
            entity.addFilter(new Filter("id", "is null", null));
        }
        return super.findPage(entity);

    }

}
